<template>
  <div class="data-table-group" @mouseenter="mouseenter" @mouseleave="mouseleave">
    <el-table
        ref="TestTable"
        :data="lists"
        class="yt-table"
        border
        height="100%"
        :row-class-name="rowClassName">
      <el-table-column prop="name" label="巡检机名称"></el-table-column>
      <el-table-column prop="targetName" label="目标名称"></el-table-column>
      <el-table-column prop="airspeed" label="速度"></el-table-column>
      <el-table-column prop="height" label="高度"></el-table-column>
      <el-table-column prop="latitude" label="经度"></el-table-column>
      <el-table-column prop="longitude" label="纬度"></el-table-column>
      <el-table-column prop="date" label="时间"></el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" icon="" @click="sendComd">发送指令</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "DataTable",
  props: {
    data: {
      type: Object,
      default: () => {
        return {}
      }
    },
    datas: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      lists: [],
      isEnter: false,
      timer: null
    }
  },
  watch: {
    data(val) {
      clearInterval(this.timer)
      this.lists.push(val.data)
      this.$nextTick(() => {
        if (!this.isEnter) {
          this.backtop()
        }
      })
    }
  },
  mounted() {
    this.lists = JSON.parse(JSON.stringify(this.datas))
    setTimeout(() => {
      this.$nextTick(() => {
        if (!this.isEnter) {
          this.backtop()
        }
      })
    }, 30)
  },
  methods: {
    backtop() {
      this.timer = setInterval(() => {
        let height = this.$refs.TestTable.bodyWrapper.offsetHeight
        let osHeight = this.$refs.TestTable.bodyWrapper.scrollHeight
        let osTop = this.$refs.TestTable.bodyWrapper.scrollTop
        let ispeed = Math.floor((osHeight - height - osTop + 5) / 5)
        this.$refs.TestTable.bodyWrapper.scrollTop = osTop + ispeed
        if (osTop === (osHeight - height)) {
          clearInterval(this.timer)
        }
      }, 30)
    },
    // 获取登录日志列表
    rowClassName({row, rowIndex}) {
      if (row.alarmLevel === 1) {
        return 'text-color-red'
      } else if (row.alarmLevel === 2) {
        return 'text-color-orange'
      } else if (row.alarmLevel === 3) {
        return 'text-color-yellow'
      } else if (row.alarmLevel === 4) {
        return 'text-color-blue'
      }
    },
    mouseenter() {
      this.isEnter = true
    },
    mouseleave() {
      this.isEnter = false
    },
    sendComd() {
      this.$api.appliesControlRun({appId: this.$route.query.appid}).then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg)
        } else {
          this.$message.warning(res.data.msg)
        }
      }).catch((err) => {
        this.$message.warning(err.data.msg)
      })
    }
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer)
    }
  }
}
</script>

<style scoped lang="scss">
  .data-table-group {
    width: 100%;
    height: 100%;
  }

</style>